package com.yxj.securityhello.config;

import com.yxj.securityhello.filter.JwtAuthenticationTokenFilter;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author yangxuejiao
 * @description
 * @createDate 2025/11/1 00:56
 */
@Configuration
@EnableMethodSecurity
@EnableWebSecurity
public class SecurityConfig {
    //自定义的用于认证的过滤器，进行jwt的校验认证
    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

//    @Bean
//    public UserDetailsService userDetailsService(PasswordEncoder encoder) {
//        UserDetails user = User.withUsername("user")
//                .password(encoder.encode("123456"))
//                .roles("USER")
//                .build();
//        return new InMemoryUserDetailsManager(user);
//    }
//
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    //调用 AuthenticationManager.authenticate
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
        return config.getAuthenticationManager();
    }


    //SecurityFilterChain 一个表示安全过滤器链的对象，所有请求在进入 Controller 之前，都会先经过这里配置的安全规则。
    //配置 1:登录请求放行
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(auth -> auth
                .requestMatchers("/user/login").permitAll()
                .anyRequest().authenticated()
                //前后端分离架构，关闭默认页面，改成自己写的登录接口
                //Spring Security 默认开启 CSRF 防护，只允许浏览器携带特定 token 的 POST 请求，但前后端分离场景下，CSRF 对我们没必要，反而会拦截正常的 JSON 登录请求。
        ).formLogin(form -> form.disable())
                .csrf(csrf -> csrf.disable())
                //启用 HTTP Basic 认证，客户端在请求头带上：Authorization: Basic base64(username:password)，Spring Security 会自动解析并校验用户名密码。
                .httpBasic(Customizer.withDefaults());

        //配置过滤器的执行顺序
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //允许跨域
        return http.build();
    }
}
